var app = getApp();
Page({
  data: {
    curRechargeMoney:''
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.from){
      that.setData({
        from:e.from
      })
    }
    if (e && e.type) {
      that.setData({
        cztype: e.type
      })
    };
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestRemainRecharge();
    }
  },
  onShow: function () {
    app.setNavtitle('账户余额充值');
  },
  requestRemainRecharge:function(id){
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_member_recharge_cfg'
      },
      success: function (res) {
        console.log(res);
        if (res.data.ec == 200) {
          var rechargeList = res.data.data.list;
          for (var i = 0; i < rechargeList.length;i++){
            rechargeList[i].isShowdesc = false;
          }
          that.setData({
            remainRecharge: res.data.data,
            rechargeList: rechargeList
          })
          console.log(that.data.remainRecharge);
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  toggleShowdesc:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var rechargeList = that.data.rechargeList;
    rechargeList[index].isShowdesc = !rechargeList[index].isShowdesc;
    that.setData({
      rechargeList: rechargeList
    })
  },
  moneyChange:function(e){
    var that = this;
    this.setData({
      cusMoney:e.detail.value
    })
    if (that.data.cusMoney>0){
      that.setData({
        curRechargeMoney: ''
      })
    }
  },
  rechargeToggle:function(e){
    var that = this;
    var rechargeMoney = e.currentTarget.dataset.recharge;
    that.setData({
      curRechargeMoney: rechargeMoney,
      cusMoney:''
    })
    console.log(that.data.curRechargeMoney);
  },
  chooseRecharge:function(e){
    var that = this;
    var rechargeMoney = e.currentTarget.dataset.recharge;
    that.setData({
      curRechargeMoney: rechargeMoney,
      cusMoney: ''
    })
    that.nowRecharge();
  },
  nowRecharge: function () {
    var that = this;
    app.getSubId(that, 'applet_member_recharge_pay', 'toNowRecharge');
  },
  toNowRecharge:function(){
    var that = this;
    var data = {};
    data.map ='applet_member_recharge_pay';
    data.pid = '';
    data.price = '';
    var curRechargeMoney = that.data.curRechargeMoney;
    if (curRechargeMoney.id){
      data.pid = curRechargeMoney.id;
      data.price = curRechargeMoney.money;
    }
    if (that.data.cusMoney>0){
      data.price = that.data.cusMoney;
    }
    if (!data.price>0){
      app.errorTip(that, "充值金额不合法", 2000);
      return;
    }
    console.log(data);
    that.requestRecharge(data);
  },
  requestRecharge: function (data) {
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              that.requestMemberInfo();
            },
            'fail': function (res) {
              console.log(res);
            }
          });
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestMemberInfo: function () {
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map:'applet_member_info'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          console.log(res.data.data);
          app.globalData.userInfo = res.data.data;
          if (that.data.from=='slide'){
            wx.navigateTo({
              url: '/subpages/myBank/myBank',
            })
          }else{
            wx.navigateBack({
              delta: 1
            })
          }
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})